﻿//为了不和以前冲突，所以加一个和初始化SWF一样的方法。
var HadBeenSWFUploadLimit = 60
function InitUploadComponent() {
    //var limit = 6;
    HadBeenSWFUpload = new SWFUpload({
        // Backend Settings
        upload_url: "http://member.lotour.com/manage/Forms/Upload.ashx?immediate=1",

        // File Upload Settings
        file_size_limit: "30 MB",
        file_types: "*.jpg;*.png;*.jpeg;*.gif",
        file_upload_limit: "0",    // Zero means unlimited

        // Event Handler Settings - these functions as defined in Handlers.js
        //  The handlers are not part of SWFUpload but are part of my website and control how
        //  my website reacts to the SWFUpload events.

        //file_queue_limit: limit,
        file_queue_limit: HadBeenSWFUploadLimit,
        file_queue_error_handler: HadBeenFileQueueError,
        file_dialog_complete_handler: HadBeenFileDialogComplete,
        upload_start_handler: HadBeenUploadStart,
        file_queued_handler: HadBeenUploadQueued,
        upload_progress_handler: HadBeenUploadProgress,
        upload_error_handler: HadBeenUploadError,
        upload_success_handler: HadBeenUploadOK,
        upload_complete_handler: HadBeenUploadComplete,

        // Button settings
        //button_image_url: "http://img1.lotour.com/wantgo/bgpic.gif",

        button_placeholder_id: "uploadButtonPlaceHolder",
        button_width: 127,
        button_height: 34,
        button_text_top_padding: 1,
        button_text_left_padding: 5,
        button_cursor: SWFUpload.CURSOR.HAND,
        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,

        custom_settings: {
            upload_target: "divFileProgressContainer"
        },
        // Flash Settings
        flash_url: "http://js.lotour.com/im/swfupload.swf", // Relative to this file
        // Debug Settings
        debug: false
    });
}

function HadBeenFileQueueError(file, errorCode, message) {
    try {
        var imageName = "error.gif";
        var errorName = "";
        if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) {
            errorName = "亲，只能上传前6张照片！";
        }

        if (errorName !== "") {
            return;
        }
        //alert(errorCode);
        switch (errorCode) {
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                imageName = "zerobyte.gif";
                break;
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                imageName = "toobig.gif";
                alert("亲，图片超大了，最大30M哦！");
                break;
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
            default:
                //alert(message);
                alert("亲，图片太多，最多上传6张。并且每张不能超过30M哦！");
                break;
        }
    } catch (ex) {
        this.debug(ex);
    }

}
function HadBeenFileDialogComplete(numFilesSelected, numFilesQueued) {
    try {
        if (numFilesQueued > 0) {
            this.startUpload();
        }
    } catch (ex) {
        this.debug(ex);
    }
}

function HadBeenUploadQueued(file) {
    var que = this.getStats().files_queued;
    if ((HadBeenSWFUploadLimit <= 0) && (HadBeenSWFUploadLimit-que<0)) {
        this.cancelUpload(file.id, false);
    }
//    if (HadBeenSWFUploadLimit==0) {
//        alert("亲，图片只能上传6张哦！");
//    }
    HadBeenSWFUploadLimit = HadBeenSWFUploadLimit - 1;
}

function HadBeenUploadStart(file) {
}

function HadBeenUploadProgress(file, bytesLoaded) {
    $("#hadbeenuploading").removeClass("spl");
}

function HadBeenUploadOK(file, serverData) {
       try {
        setTimeout(function () {
            serverData = eval('(' + serverData + ')');
            if ($.trim(serverData.err) == "") {
                var url = serverData.msg.url;
                var localname = serverData.msg.localname;
                if (url.substr(0, 1) === '!') url = url.substr(1);
                try {
                    localname = decodeURIComponent(localname);
                }
                catch (err)
                { }
                //$("<li>asdfasfasf</li>").appendTo("#fa_img_list");
                $("<li><div class=\"f_add img_box\"> <a onmouseout=\"$(this).find('span').css({'display':'none'})\" onmouseover=\"$(this).find('span').css({'display':'inline'})\" href=\"javascript:void(0);\" class=\"del\"><img width=\"86\" height=\"86\" src=\"" + url + "\" alt=\"" + localname + "\"><span onclick=\"delimg(this)\" style=\"display: none;\">删除</span></a></div></li>").appendTo("#fa_img_list");
                $('.hsimg').find('.hsimgbg').unbind("hover");
                $('.hsimg').find('.hsimgbg').hover(function () {
                    var _em = $(this).find('em');
                    var _img = _em.siblings('img');
                    var _li = $(this).parent();
                    if (_img.length > 0) {
                        _em.removeClass("spl");
                        _em.unbind("click");
                        _em.click(function () {
                            if (HadBeenSWFUploadLimit < 0) {
                                HadBeenSWFUploadLimit = 0;
                            }
                            HadBeenSWFUploadLimit = HadBeenSWFUploadLimit + 1;
                            if (HadBeenSWFUploadLimit > 0) {
                                $('.hsaddimg').show();
                            }
                            _li.remove();
                            _em.addClass("spl");
                            var imgNum = $('#fa_img_list').find('img').length;
                            if (imgNum < 6) {
                                $('.hsaddimg').removeClass("spl");
                            }
                        })
                    }
                }, function () {
                    var _em = $(this).find('em');
                    _em.addClass("spl");
                })
            }
            if ($('#fa_img_list li').length == 6) {
                $('.hsaddimg').hide();
            }
        }, 500);
    } catch (ex) {
        this.debug(ex);
    }
}

function HadBeenUploadComplete(file) {
    try {
        /*  I want the next upload to continue automatically so I'll call startUpload here */
        if (this.getStats().files_queued > 0) {
            this.startUpload();
        } else {
            $("#hadbeenuploading").addClass("spl");
        }
    } catch (ex) {
        this.debug(ex);
    }
}

function HadBeenUploadError(file, errorCode, message) {
    var imageName = "error.gif";
    var progress;
    try {
        switch (errorCode) {
            case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
            case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                imageName = "uploadlimit.gif";
                break;
            default:
				 $(errorCode).appendTo("#fa_img_list");
               // $("<li>上传图片失败，请稍后重试或者选择高版本的浏览器!</li>").appendTo("#fa_img_list");
                break;
        }

    } catch (ex3) {
        this.debug(ex3);
    }

}